<?php
/**
 * 节点逻辑类
 * @author zengnz
 *
 */
class NodeLogic extends CommonModel
{
	//所有节点缓存
	const ALL_NODE = 'ALL_NODE';
	
	// 缓存模型类
	private $_CacheModel = '';
	
	public function __construct() {
		$this -> _CacheModel = D('Cache', 'Service');
		$this -> _CacheModel -> init();
	}
	
	/**
	 * 获取所有节点
	 */
	public function getAllNodes() {
		// 用户ID
		$uId = $this -> getLoginId();
		$key = self::ALL_NODE.'_'.$uId;
		
		$nodeList = $this -> _CacheModel -> get($key);
		
		if (!$nodeList) {
			$Node = D('Node');
			
			$nodeList = array();
			$allNodes = array();
			$nodes = $Node -> where('status=1') -> order('sort ASC') -> select();
			if ($nodes) {
				foreach($nodes as $key => $val) {
					$allNodes[$val['pid']][$val['id']] = $val;
				}
			
				foreach ($allNodes[0] as $key => $val) {
					$nodeList[$val['id']] = array(
							'title' => $val['title'],
							'id' => $val['id'],
							'subNodes' => $allNodes[$val['id']]
					);
				}
			}
			
			$this -> _CacheModel -> set($key, $nodeList);
			$this -> _CacheModel -> saveKeys($key, 'user', $uId);
		}
		
		return $nodeList;
	}
	
	
}